<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.ww78.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: 快乐是福 <815856515@qq.com>
// +----------------------------------------------------------------------
// | QQ: 815856515
// +----------------------------------------------------------------------
// | 如需修改和引用，请保留此头部信息！

namespace Mz\Model;
class Qqlogin extends \Think\Model {
	public $json;

	public function __construct($uin,$pwd,$code=0,$sig=0){
		$this->uin=$uin;
		$this->pwd=$pwd;
		$this->code=$code;
		$this->sig=$sig;
		if($code){
			$this->dovc();
		}else{
			$this->checkvc();
		}
	}
	public function getsid($url){
		$ret = $this->get_curl($url,0,0,0,1);
		if(preg_match("/Location:(.*)\r\n/iU", $ret,$u)){
			$sid=explode('sid=',$u[1]);
			$sid=explode('&',$sid[1]);
			return trim($sid[0]);
		}elseif(
			preg_match("/sid=(.{24})/iU", $ret,$u)){
			return trim($u[1]);
		}else{
			return $this->getsid3($url);
		}
	}
	public function getsid3($url,$do=0){
		$do++;
		if($ret = $this->get_curl($url,0,1)){
			if(preg_match('/\{\"sid\":\"(.{24})\"/iU', $ret,$sid)){
				return $sid[1];
			}else{
				if($do<5){
					return $this->getsid3($url,$do);
				}else{
					$this->sidcode=2;
					return false;
				}
			}
		}else{
			$this->sidcode=1;
			return false;
		}
	}
	public function login(){
		if(strpos('s'.$this->code,'!')){
			$v1=0;
		}else{
			$v1=1;
		}
		$p=$this->getp();
		$url='http://ptlogin2.qzone.com/login?verifycode='.$this->code.'&u='.$this->uin.'&p='.$p.'&pt_randsalt=0&ptlang=2052&low_login_enable=0&u1=http%3A%2F%2Fm.qzone.com%2Finfocenter%3Fg_f%3D&from_ui=1&fp=loginerroralert&device=2&aid=549000929&pt_ttype=1&pt_3rd_aid=0&ptredirect=1&h=1&g=1&pt_uistyle=9&pt_vcode_v1='.$v1.'&pt_verifysession_v1='.$this->sig.'&';
		$ret = $this->get_curl($url,0,0,0,1);
		if(preg_match("/ptuiCB\('(.*?)'\);/", $ret, $arr)){
			$r=explode("','",$arr[1]);
			if($r[0]==0){
				preg_match('/skey=@(.{9});/',$ret,$skey);
				if($sid=$this->getsid($r[2])){//本地
				//if($sid=$this->getsid2($r[2])){//调用
					$this->json='{"code":0,"uin":"'.$uin.'","sid":"'.$sid.'","skey":"@'.$skey[1].'"}';
				}else{
					$this->json='{"code":-3,"msg":"登陆成功，获取SID失败！'.$this->sidcode.'"}';
				}
			}elseif($r[0]==4){
				$this->json='{"code":-3,"msg":"验证码错误!"}';
				$this->checkvc();
			}elseif($r[0]==3){
				$this->json='{"code":-3,"msg":"密码错误！"}';
			}elseif($r[0]==19){
				$this->json='{"code":-3,"msg":"您的帐号暂时无法登录，请到 http://aq.qq.com/007 恢复正常使用！"}';
			}else{
				$this->json='{"code":-3,"msg":"'.str_replace('"','\'',$r[4]).'"}';
			}
		}else{
			$this->json='{"code":-3,"msg":"'.$ret.'"}';
		}
	}
	public function getp(){
		$url="http://qqapp.aliapp.com/?uin=".$this->uin."&pwd=".strtoupper(md5($this->pwd))."&vcode=".strtoupper($this->code);
		$getp=$this->get_curl($url);
		if($getp){
			return $getp;
		}else{
			exit("<script language='javascript'>alert('获取P值失败，请稍候重试！');history.go(-1);</script>");
		}
	}
	public function dovc(){
		$url='http://captcha.qq.com/cap_union_verify?aid=549000929&uin='.$this->uin.'&captype=8&ans='.$this->code.'&sig='.$this->sig.'&0.960725'.time();
		$data=$this->get_curl($url,0,1);
		if(preg_match("/cap\_InnerCBVerify\((.*?)\);/", $data, $json)){
			$json=str_replace(array('{',':',','),array('{"','":',',"'),$json[1]);
			$arr=json_decode($json,true);
			$randstr=$arr['randstr'];
			$sig=$arr['sig'];
		}
		if($randstr){
			$this->code=$randstr;
			$this->sig=$sig;
			$this->login();
		}else{
			$this->getvc($this->sig);
		}
	}
	public function checkvc(){
		$url='http://check.ptlogin2.qzone.com/check?pt_tea=1&uin='.$this->uin.'&appid=549000929&ptlang=2052&r=0.071823'.time();
		$data=$this->get_curl($url,0,1);
		if(preg_match("/ptui_checkVC\('(.*?)'\);/", $data, $arr)){
			$r=explode("','",$arr[1]);
			if($r[0]==1){
				$this->json='{"code":-2,"sig":"'.$r[1].'"}';
				$this->getvc($r[1]);
			}else{
				$this->code=$r[1];
				$this->sig=$r[3];
				$this->login();
			}
		}else{
			exit("<script language='javascript'>alert('判断是否有验证码失败，请稍候重试！');history.go(-1);</script>");
		}
	}
	
	public function getvc($sig){
		if(strlen($sig)==56){
			$url='http://captcha.qq.com/cap_union_show?captype=3&aid=549000929&uin='.$this->uin.'&cap_cd='.$sig.'&v=0.0672220'.time();
			$data=$this->get_curl($url,0,1);
			if(preg_match("/g\_click\_cap\_sig=\"(.*?)\";/", $data, $arr)){
				$this->json='{"code":-1,"sig":"'.$arr[1].'"}';
			}else{
				exit("<script language='javascript'>alert('获取验证码失败，请稍候重试！');history.go(-1);</script>");
			}
		}else{
			$url='http://captcha.qq.com/getQueSig?aid=549000929&uin='.$this->uin.'&captype=8&sig='.$sig.'&0.819966'.time();
			$data=$this->get_curl($url);
			if(preg_match("/cap_getCapBySig\(\"(.*?)\"\);/", $data, $arr)){
				$this->json='{"code":-1,"sig":"'.$arr[1].'"}';
			}else{
				exit("<script language='javascript'>alert('获取验证码失败，请稍候重试！');history.go(-1);</script>");
			}
		}
	
	}

	private function get_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		$klsf[] = "Accept:application/json"; 
		$klsf[] = "Accept-Encoding:gzip,deflate,sdch"; 
		$klsf[] = "Accept-Language:zh-CN,zh;q=0.8"; 
		$klsf[] = "Connection:keep-alive"; 
		curl_setopt($ch, CURLOPT_HTTPHEADER, $klsf);
		if($post){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}
		if($header){
			curl_setopt($ch, CURLOPT_HEADER, TRUE);
		}
		if($cookie){
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}
		if($referer){
			if($referer==1){
				curl_setopt($ch, CURLOPT_REFERER, "http://m.qzone.com/infocenter?g_f=");
			}else{
				curl_setopt($ch, CURLOPT_REFERER, $referer);
			}
		}
		if($ua){
			curl_setopt($ch, CURLOPT_USERAGENT,$ua);
		}else{
			curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0');
		}
		if($nobaody){
			curl_setopt($ch, CURLOPT_NOBODY,1);//主要头部
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
		}
		curl_setopt($ch, CURLOPT_ENCODING, "gzip");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$ret = curl_exec($ch);
		curl_close($ch);
		return $ret;
	}

}